<?php
/**
 * Created by PhpStorm.
 * User: 庆金
 * Date: 2019-12-12
 * Time: 22:19
 */
//创建Server对象，监听 127.0.0.1:9501端口
$serv = new Swoole\Server("127.0.0.1", 9501);

$serv->set([
    // worker进程数
    'worker_num'    => 4,
    //最大连接数
    'max_request'   =>  10000,
]);

//监听连接进入事件
/**
 * $fd 客户端连接的唯一标识
 * $recator_id 线程id
 */
$serv->on('Connect', function ($serv, $fd, $recator_id) {
    echo "Client: 线程ID：{$recator_id} - 客户端连接ID： {$fd} Connect.\n";
});

//监听数据接收事件
$serv->on('Receive', function ($serv, $fd, $recator_id, $data) {
    $serv->send($fd, "Server: 线程ID：{$recator_id} - 客户端连接ID： {$fd} ".$data);
});

//监听连接关闭事件
$serv->on('Close', function ($serv, $fd) {
    echo "Client: Close.\n";
});

//启动服务器
$serv->start();